package com.hly.versioncontroladmin.config;

import com.hly.versioncontroladmin.Interceptor.DownHandlerInterceptor;
import com.hly.versioncontroladmin.utils.FileUtils;
import com.hly.versioncontroladmin.utils.PathUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    private final PathUtils pathUtils;
    private final DownHandlerInterceptor downHandlerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(downHandlerInterceptor).addPathPatterns(pathUtils.resourcesPath + "/**");
    }

    /**
     * 添加静态资源文件，外部可以直接访问地址
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 创建项目需要的文件夹
        FileUtils.files(pathUtils.path);

        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler(pathUtils.resourcesPath + "/**")
                .addResourceLocations("file:" + pathUtils.path.replace("\\","/") + "/");
    }
}
